/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.core.refi18n;

import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.MetadataProject;
import com.inspur.edp.lcm.metadata.api.service.FileService;
import com.inspur.edp.lcm.metadata.common.Utils;
import com.inspur.edp.lcm.metadata.core.MetadataCoreManager;
import com.inspur.edp.lcm.metadata.core.MetadataProjectCoreService;
import com.inspur.edp.lcm.metadata.core.index.ProjectMetadataCacheService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RefLocationFilter implements IRefI18nFilter {
    private final MetadataCoreManager metadataManager = new MetadataCoreManager();
    private final MetadataProjectCoreService projectService = new MetadataProjectCoreService();

    @Override
    public List<GspMetadata> getRefI18nMetadata(String resourceMdID, String resourceFileName, String path) {
        //从工程引用范围内，查找包依赖的元数据内容
        MetadataProject metadataProject = MetadataProjectCoreService.getCurrent().getMetadataProjInfo(path);
        Utils.checkNPE(metadataProject, "获取的元数据工程信息为null，参数为projPath：" + path);

        // 从当前工程及bo下的引用的工程下搜索
        List<GspMetadata> metadataFromProjects = getMetadataFromProjects(resourceMdID, metadataProject);
        if (metadataFromProjects.size() > 0) {
            return metadataFromProjects;
        }

        // 从缓存中获取
        ProjectMetadataCacheService projectMetadataCacheService = ProjectMetadataCacheService.getNewInstance(metadataProject, path);
        return projectMetadataCacheService.getRefLresMetadata(resourceMdID, resourceFileName);
    }

    private List<GspMetadata> getMetadataFromProjects(String resourceMdID, MetadataProject metadataProject) {
        List<GspMetadata> lresMetadataList = new ArrayList<>();
        Set<String> refProjPaths = new HashSet<>();
        if (metadataProject != null) {
            refProjPaths.add(metadataProject.getProjectPath());
            projectService.getRefProjPaths(metadataProject.getProjectPath(), refProjPaths);
            FileService fileService = SpringBeanUtils.getBean(FileService.class);
            for (String projPath : refProjPaths) {
                List<GspMetadata> metadataList = metadataManager.getMetadataList(projPath);
                for (GspMetadata gspMetadata : metadataList) {
                    if (gspMetadata.getHeader().getId().equals(resourceMdID)) {
                        String resourceMdFileNameWithoutExt = fileService.getFileNameWithoutExtension(gspMetadata.getHeader().getFileName());
                        for (GspMetadata metadata : metadataList) {
                            if (metadata.getHeader().getFileName().endsWith(Utils.getLanguageMetadataFileExtention()) && metadata.getHeader().getFileName().startsWith(resourceMdFileNameWithoutExt)) {
                                GspMetadata lresMetadata = metadataManager.loadMetadata(gspMetadata.getHeader().getFileName(), gspMetadata.getRelativePath());
                                lresMetadataList.add(lresMetadata);
                            }
                        }
                        break;
                    }
                }
            }
        }
        return lresMetadataList;
    }
}
